package com.we823.cms.controller.manage.cms;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.we823.cms.common.web.pageable.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.we823.cms.common.WebConstants;
import com.we823.cms.common.utils.RequestUtil;
import com.we823.cms.common.web.Oper;
import com.we823.cms.controller.manage.AbstractSimpleManageController;
import com.we823.cms.model.cms.NewsChannel;
import com.we823.cms.repository.service.cms.NewsChannelService;

@Controller
@RequestMapping(WebConstants.WEB_PREFIX+WebConstants.WEB_MANAGE_URL+"/news_channel")
public class NewsChannelManageController extends AbstractSimpleManageController<NewsChannel, Integer> {

	private String baseUrl = WebConstants.WEB_MANAGE_TEMPLATE_BaseUrl+"news_channel/";
	
	private NewsChannelService newsChannelService;
	
	@Autowired
	public NewsChannelManageController(NewsChannelService newsChannelService) {
		super(newsChannelService);
		this.newsChannelService = newsChannelService;
		super.baseUrl = baseUrl;
	}

	@Override
	public PageInfo<NewsChannel> getData(PageRequest pageRequest, HttpServletRequest request, Model model, boolean pageable) {
		PageInfo<NewsChannel> newsChannelPage = null;
		if(pageable){
			newsChannelPage = newsChannelService.findAll(pageRequest);
		}else{
			newsChannelPage = new PageInfo<>(newsChannelService.findAll());
		}
		
		return newsChannelPage;
	}

	@Override
	public NewsChannel getEntity(HttpServletRequest request, Integer id,Oper oper) {
		String name = RequestUtil.getValueByName(request, "name");
		Integer orderid = RequestUtil.getValueByName(request, "orderid", 0);
		Integer templateId = RequestUtil.getValueByName(request, "templateId", 0);
		
		NewsChannel newsChannelModel = null;
		if(id==null || id==0){
			newsChannelModel = new NewsChannel();
			newsChannelModel.setName(name);
			newsChannelModel.setOrderid(orderid);
			newsChannelModel.setAddtime(new Date());
			newsChannelModel.setTemplateId(templateId);
		}else{
			newsChannelModel = newsChannelService.findOne(id);
			if(newsChannelModel!=null){
				newsChannelModel.setId(id);
				newsChannelModel.setName(name);
				newsChannelModel.setOrderid(orderid);
				newsChannelModel.setEdittime(new Date());
				newsChannelModel.setTemplateId(templateId);
			}else{
				newsChannelModel = new NewsChannel();
				newsChannelModel.setId(id);
				newsChannelModel.setName(name);
				newsChannelModel.setOrderid(orderid);
				newsChannelModel.setAddtime(new Date());
				newsChannelModel.setTemplateId(templateId);
			}
		}

		return newsChannelModel;
	}

	@Override
	public void setIndetity() {
		setResourceIdentity("cms:news");
	}

}
